package org.glut.AppStoreProject.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.glut.AppStoreProject.entity.App;
import org.glut.AppStoreProject.service.AppServiceImpl;
import org.glut.AppStoreProject.vo.SearchTagAppVO;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.*;


@WebServlet(value = {"/toSearch"})
public class ToSearchServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        AppServiceImpl appService = new AppServiceImpl();
        Gson gson = null;
        Map<String,Object> result = new HashMap<>();
        result.put("hot", appService.getTop5("hot",null));
        result.put("more", appService.getTop5("more",null));
        result.put("game", appService.getTop5("game",2));
        result.put("rank", appService.getRank10("rank"));
        gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .setPrettyPrinting()
                .create();
        String respJson = gson.toJson(result);
        writer.write(respJson);

    }

    /**
     * @param req
     * @param resp
     * @description:
     * @author: gluglu2
     * @date: 2025/7/14 12:46
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
